//
//  ReisgterView.m
//  groupProject
//
//  Created by mac on 15/12/17.
//  Copyright © 2015年 mac. All rights reserved.
//

#import "ReisgterView.h"
#import "Config.h"
#import "LoginView.h"
#import "PassLoginView.h"
#import "MyMessageBox.h"
#import "AFHTTPRequestOperationManager.h"

#define ZM_SERVER_LOGIN @"http://115.28.55.133:8000/jsonlogin"
#define ZM_SERVER_REGISTER @"http://115.28.55.133:8000/jsonregister"
@interface ReisgterView ()<UITextFieldDelegate>

@end

@implementation ReisgterView{
    UITextField *nameField;
    UITextField *passField;
    UIButton  * ResBtn;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor =[UIColor whiteColor];
    self.navigationItem.title = @"注册";
    
    if (self.ReisgterSwitchModel == YES) {
        //左Button
        UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc]initWithTitle:@"关闭"style:UIBarButtonItemStylePlain target:self action:@selector(backView)];
        [leftBtn setTintColor:[UIColor redColor]];
        self.navigationItem.leftBarButtonItem = leftBtn;
        //右Button
        UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc]initWithTitle:@"登录"style:UIBarButtonItemStylePlain target:self action:@selector(BtnLogin)];
        self.navigationItem.rightBarButtonItem = rightBtn;
    }
    [self addLabel];
    [self addTextField];
    [self AddButton];
    
}
-(void)addLabel{
    UILabel * label1 =[[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*0.01, SCREEN_HEIGHT*0.2, SCREEN_WIDTH*0.3, SCREEN_WIDTH*0.1)];
    label1.text = @"请输入姓名";
    label1.textColor =[UIColor blackColor];
    [self.view addSubview:label1];
    UILabel * label2 =[[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*0.01, SCREEN_HEIGHT*0.3, SCREEN_WIDTH*0.3, SCREEN_WIDTH*0.1)];
    label2.text = @"请输入密码";
    label2.textColor =[UIColor blackColor];
    [self.view addSubview:label2];
}

-(void)addTextField{
    nameField = [[UITextField alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*0.3, SCREEN_HEIGHT*0.2, SCREEN_WIDTH/3.0*2, SCREEN_WIDTH*0.1)];
    
    nameField.borderStyle = UITextBorderStyleRoundedRect;
    
    nameField.keyboardType =  UIKeyboardTypeASCIICapable;
    nameField.delegate = self;
    [self.view addSubview:nameField];
    
    passField = [[UITextField alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*0.3, SCREEN_HEIGHT*0.3, SCREEN_WIDTH/3.0*2, SCREEN_WIDTH*0.1)];
    passField.borderStyle = UITextBorderStyleRoundedRect;
    passField.keyboardType =  UIKeyboardTypeASCIICapable;
    passField.delegate = self;
    [self.view addSubview:passField];
}
-(void)AddButton{
    ResBtn = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*0.05, SCREEN_HEIGHT*0.4, SCREEN_WIDTH*0.9, SCREEN_WIDTH*0.1)];
    [ResBtn setTitle:@"确认注册" forState:UIControlStateNormal];
    [ResBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    ResBtn.backgroundColor = [UIColor redColor];
    [ResBtn.layer setMasksToBounds:YES];
    [ResBtn.layer setCornerRadius:10.0];//设置矩形四个圆角半径
    [ResBtn.layer setBorderWidth:1.0];//边框宽度
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 1, 0, 0, 1 });
    [ResBtn.layer setBorderColor:colorref];//边框颜色
    [self.view addSubview:ResBtn];
    [ResBtn addTarget:self action:@selector(ResBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
}
-(void)ResBtnClick{
    AFHTTPRequestOperationManager *manager;
    manager = [AFHTTPRequestOperationManager manager];
    NSString *username = nameField.text;
    NSString *password = passField.text;
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:username,@"username",password,@"password",nil];
    [ manager POST:ZM_SERVER_REGISTER parameters:dic success:^(AFHTTPRequestOperation *operation,id response)
     {
         NSDictionary *dic = (NSDictionary *)response;
         NSNumber *code = [dic objectForKey:@"code"];
         if (code.intValue == 200) {
             NSString *userName = [dic objectForKey:@"username"];
             NSString *tmpstr = [NSString stringWithFormat:@"%@注册成功!",userName];
             [MyMessageBox showMessage:tmpstr duration:1];
         }
         else
         {
             NSString *tmpStr = [[dic objectForKey:@"msg"]objectForKey:@"message"];
             
             [MyMessageBox showMessage:tmpStr duration:1];
         }
     } failure:^(AFHTTPRequestOperation *operation,NSError *error)
     {
         [MyMessageBox showMessage:@"客官，网络出错！" duration:2];
     }];
    
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    //收起键盘取消第一响应者
    [textField resignFirstResponder];
    return YES;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    //收起键盘--点击边框外部
    [self.view endEditing:YES];
}

//返回登陆界面
-(void)backView{
    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
}
//点击进入登录界面
-(void)BtnLogin{
    LoginView * vc2 = [[LoginView alloc]init];
    vc2.LoginSwitchModel = NO;
    [self.navigationController pushViewController:vc2 animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
